package com.example.comitclx1.controller;

import com.example.comitclx1.pojo.Result;
import com.example.comitclx1.utils.AliOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Slf4j
@RestController
public class UploadController {
    @Autowired
    private AliOSSUtils aliOSSUtils;
    @PostMapping("/upload")
    public Result upload(MultipartFile image) throws IOException {
        log.info("文件上传,文件名{}",image.getOriginalFilename());
        String url = aliOSSUtils.upload(image);
        log.info("url={}",url);
        return Result.success(url);
    }
}
/**
 * 本地上传
 */
 /*  public Result upload(String username, Integer age, MultipartFile image) throws Exception {
        log.info("文件上传 {},{},{}",username,age,image);
        int i = image.getOriginalFilename().lastIndexOf(".");
        String extname = image.getOriginalFilename().substring(i);
        String s = UUID.randomUUID().toString() + extname;
        image.transferTo(new File("D:\\images\\"+s));
        return Result.success();
    }*/